package com.cloud.mqtt.monitor;

import com.cloud.mqtt.Emq.MQTTServer;
import com.cloud.mqtt.util.CommonVariable;
import com.cloud.mqtt.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import java.math.BigDecimal;
import java.math.RoundingMode;


/**
 * @program: mqtt
 * @description: 数据模拟器
 * @author: Cloud
 * @create: 2021/9/23 15:45:41
 */
@Slf4j
public class DataMonitor {

    public static void runDataMonitorTask() {
        log.info("开始模拟数据........");
        MQTTServer mqttServer = SpringContextUtils.getBean(MQTTServer.class);
        while (true) {
            System.out.println("本次模拟的数据为：");
            // 发送模拟的数据
            BigDecimal temperature = new BigDecimal(CommonVariable.random.nextDouble()+1).setScale(2, RoundingMode.HALF_UP);
            int random = (CommonVariable.random.nextInt(1)+1);
            int deviceNum = (CommonVariable.random.nextInt(4)+1);
            int power = (CommonVariable.random.nextInt(50)+20);
            boolean[] isAlert = {true,false};
            boolean[] isRun = {true,false};
//            String message = "{\"deviceNum\":\"device0"+deviceNum+"\",\"temperature\":"+temperature+", \"isAlert\":"+isAlert[random]+", \"power\":"+power+", \"isRun\":"+isRun[random]+", \"ts\":"+new Date().getTime()+"}";
            String message = "{\"deviceNum\":\"device0"+deviceNum+"\",\"temperature\":"+temperature+", \"isAlert\":"+isAlert[random]+", \"power\":"+power+", \"isRun\":"+isRun[random]+"}";
            try {
                mqttServer.sendMessage("v1/gechuang/CVD/CVD/telemetry",message);
                Thread.sleep(1000);
            } catch (MqttException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
